/*
 * 历次就诊医嘱记录
 */
//加载历次就诊医嘱记录表格
var beforeAdviceArr;
function refreashBeforeAdvice(){
	debugger;
	 var arr = [];
	 $.ajax({
		 url : rootUrl + "/doctoradvice/patientsuccessivedoctoradvicerecord",
		 type : "post",
		 dataType : 'json',
//		 async : false,
		 data :
			{
				"EmpID": bce01,
				"lVAA01": $("#vaa01").val(),
			    "lVAF04" : 1,
			    "lCheck" : 0,
			    "productid" : 100,
			    "programid" : 105002,
			    "paramno" : 233,
			    "currPage" : 1,
			    "pageSize" : 30
			},
			success : function(data){
				var temp = data.list;
				for(var i=0;i<temp.length;i++){
					var dataObj=temp[i];
					dataObj.data_info = JSON.stringify(temp[i]);
					arr.push(dataObj);
				}
				beforeAdviceArr = data.list;
				beforeAdviceInit(data.list);
			}
		
	 })
}
function beforeAdviceInit(arr)
{
	var tname = "before_advice_table";
	layer.msg('加载中....', {icon: 16,skin: 'layui-layer-msg16',shade: [0.1, '#393D49'],time:1*400}); 
	$('#before_advice_container').resize(function(){
		$("#before_advice_table").setGridWidth($("#before_advice_container").width()*1);
	});
	jQuery('#before_advice_table').GridUnload();
	jQuery("#before_advice_table").jqGrid(
	{
	datatype: 'local', 
	data:arr,
	colModel :
	[
//	{
//	label : "就诊id",
//	name : '',
//	width:"110",
//	},
	{
	label:"开始时间",
	name : 'vaf36',
	width:"160"
	},
	{
	label : "医嘱内容",
	name : 'vaf22n',
	width:"320"
	},
	{
	label : "皮试",
	name : 'vaf25',
	width:"60",
	},
	{
	label : "用法",
	name : 'fusage',
	width:"150",
	 cellattr: function(rowId, tv, rawObject, cm, rdata) {
		 
         return 'id='+tname+'_fusage' + rowId;
     }
	},
	{
	label : "总量",
	name : 'vaf21',
	width:"40",
	},
	{
	label : "单位",
	name : 'fgross',
	width:"70",
	},
	{
	label : "医生嘱托",
	name : 'vaf23',
	width:"80",
	},
	{
	label : "执行科室",
	name : 'nbck03',
	width:"75",
	},
	{
	label : "附加执行",
	name : 'bck03c',
	width:"75",
	},
	{
	label : "执行性质",
	name : 'vaf58',
	width:"80",
	},
	{
	label : "计价性质",
	name : 'vaf31',
	width:"75",
	},
	{
	label : "用药目的",
	name : 'biw02',
	width:"75",
	},
	{
	label : "医嘱备注",
	name : 'vaf55',
	width:"80",
	}, 
	{
	label : "开嘱医生",
	name : 'bce03a',
	width:"75",
	},
	{
		name : 'bby01',
		hidden:true
	},
	{
		name : 'bda01',
		hidden:true
	},
	{
		name : 'vaf01',
		hidden:true
	},
	{
		name : 'vaf22',
		hidden:true
	},
	{
		name : 'bbx01',
		hidden:true
	},
	{
		name : 'vaf32',
		hidden:true
	},
	{
		name : 'cbm01',
		hidden:true
	},
	{
		name : 'vaf36_',
		hidden:true
	}
	,
	{
		name : 'data_info',
		hidden:true
	},
	{
		name : 'vaf59',
		hidden:true
	}
	],
	viewrecords : true,
//	multiselect : true,
	height : 'auto',
	width : 'auto',
	autowidth : true,
	forceFit:true,
    shrinkToFit:true,
	rownumbers : true,
	grouping:true,
   	groupingView : 
   	{
   		groupField : ['vaf36_','cbm01'],
   		groupColumnShow : [false,false],
   		groupOrder:['desc','desc'] , 
   		groupText : ['<label class="label-normal label-spring">日期：{0}</label>','<label class="label-normal label-spring">医嘱单id：{0}</label>']
   	},
   	onSelectRow : function(rowid,status)
   	{
   	  var rowdata = $("#" + tname).jqGrid("getRowData", rowid);
		adviceDiagnose(rowdata.cbm01,"chufang_zhenduan_b");	// 处方诊断
		chufangshuxing(rowdata.vaf01,"chufang_xingzhi_b");// 处方性质
		sendAdviceHistory(rowdata.vaf01,rowdata.cbm01,"jizhu_fasong_b");//1405 医嘱单发送记录
   	},
   	loadComplete : function(data)
	{
		if (data.rows.length<1)
		{
			var $tr = $('#' + tname).find('tbody>tr:last');
			var len = $tr.find('td').length;
			$("#" + tname).html('<tr><td colspan="' + parseInt(len) + '" style="text-align:left;">没有历史医嘱数据！</td></tr>');
		}
	}, 
	gridComplete: function() {
        //在gridComplete调用合并方法,合并单元格
        mergerBeforeA(tname, 'fusage'); //用法
	}
	});
}
//医嘱类型选择,重新加载数据
function changeBeforeAdvice(){
	debugger;
		var searchStr = $("#advice_disort").val();
		 if(!!searchStr){
			 var type = searchStr.split(",");
			 var arr = [];
			 for(var i = 0;i <beforeAdviceArr.length;i++){
				 if($.inArray(beforeAdviceArr[i].bda01,type)>=0){
					 arr.push(beforeAdviceArr[i]);
				 }
			 }
			 beforeAdviceInit(arr);
		 }else{
			 beforeAdviceInit(beforeAdviceArr);
		 }
		
//	 var ids = $("#before_advice_table").jqGrid('getDataIDs');
//	 for(var j=0; j <ids.length;j++){
//		$("#before_advice_table").setRowData(ids[j],null,{display: ''});  
//	 }
//	 if(!!searchStr){
//		 var type = searchStr.split(",");
//		 for(var i=0; i <ids.length;i++){
//			 var rowdata = $("#before_advice_table").jqGrid("getRowData", ids[i]);
//			 if($.inArray(rowdata.bda01,type)<0){
//				 $("#before_advice_table").setRowData(ids[i],null,{display: 'none'});  
//			 }
//		 }
//	 }
	
}


function mergerBeforeA(gridName, CellName) {
    //得到显示到界面的id集合
    var mya = $("#" + gridName + "").getDataIDs();
    var length = mya.length;
    for (var i = 0; i < length; i++) {
        //从上到下获取一条信息
        var before = $("#" + gridName + "").jqGrid('getRowData', mya[i]);
        //定义合并行数
        var rowSpanTaxCount = 1;
        if(CellName == "copy"){
        	for (j = i + 1; j <= length; j++) {
        		//和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
        		var end = $("#" + gridName + "").jqGrid('getRowData', mya[j]);
        		if (before[CellName] == end[CellName]&&before['cbm01']==end['cbm01']&&before['vaf11']==end['vaf11']&&(before['bda01']==1||before['bda01']==2)) {
        			rowSpanTaxCount++;
        			$("#" + gridName + "").setCell(mya[j], CellName, '', { display: 'none' });
        		} else {
        			rowSpanTaxCount = 1;
        			break;
        		}
        		$("#" +gridName+"_"+ CellName + mya[i]).attr("rowspan", rowSpanTaxCount);
        	}

        }else{
        	
        	for (j = i + 1; j <= length; j++) {
        		//和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
        		var end = $("#" + gridName + "").jqGrid('getRowData', mya[j]);
        		if (before[CellName] == end[CellName]&&before['cbm01']==end['cbm01']&&before['vaf11']==end['vaf11']&&(before['bda01']==1||before['bda01']==2)&&before['vaf59']==end['vaf59']) {
        			rowSpanTaxCount++;
        			$("#" + gridName + "").setCell(mya[j], CellName, '', { display: 'none' });
        		} else {
        			rowSpanTaxCount = 1;
        			break;
        		}
        		$("#" +gridName+"_"+ CellName + mya[i]).attr("rowspan", rowSpanTaxCount);
        	}
        }
    }
}

